package com.whfp.coky.ui.base.vm

import android.content.Context
import androidx.fragment.app.FragmentActivity
import androidx.lifecycle.LifecycleOwner
import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.Observer
import androidx.recyclerview.widget.GridLayoutManager
import androidx.recyclerview.widget.RecyclerView
import androidx.viewbinding.ViewBinding
import com.hjq.widget.layout.NoScrollViewPager
import com.whfp.coky.ui.adpter.FragmentPagerAdapter
import com.whfp.coky.ui.adpter.NavigationAdapter
import com.whfp.coky.ui.base.BaseActivity
import com.whfp.coky.ui.base.BaseFragment
import com.whfp.coky.ui.base.BaseNavigationActivity
import com.whfp.coky.ui.base.BaseViewModel

/**
 *
 *
 * created by coky on 2022/3/1
 */
abstract class NavigationActVM<VB : ViewBinding> : BaseViewModel<VB>(),
    NavigationAdapter.OnNavigationListener {

    var navigationSize: Int = 0//导航控件规模个数
    var titleList:MutableList<String>? = null//标题头

    var currTitle = MutableLiveData<String>()//当前标题
    var itemList = MutableLiveData<MutableList<NavigationAdapter.MenuItem>>()//导航数据集
    var fragList = MutableLiveData<MutableList<BaseFragment<*, *>>>()//页面数据集

    var mNavigationAdapter: NavigationAdapter? = null//导航适配器
    var mPagerAdapter: FragmentPagerAdapter<BaseFragment<*, *>>? = null//页面适配器


    override fun observe(activity: BaseActivity<*, *>, owner: LifecycleOwner) {
        super.observe(activity, owner)
        currTitle.observe(activity, Observer {
           activity.getTitleBar()?.title = it
        })

        itemList.observe(activity, Observer {
            initNavigationBottom(activity)
        })
        fragList.observe(activity, Observer {
            initNavigationBody(activity)
            (activity as BaseNavigationActivity).seCurrFrag()
        })
    }

    /**
     * 底部导航初始化填充
     * adapter and recyclerview
     */
    fun initNavigationBottom(context: Context) {
//        adapter初始化
        mNavigationAdapter = NavigationAdapter(context).apply {
            setData(itemList.value!!)
//            for (index in 0 until itemList!!.size) {
//                addItem(itemList!![index])
//            }
            listener = this@NavigationActVM
            this.itemClick { pos ->
                navigationItemClick(pos)
            }
        }

//        recyclerview初始化
        getNavigationBottom().let {
            it.adapter = mNavigationAdapter
            it.layoutManager = GridLayoutManager(context, navigationSize)
        }
    }

    /**
     * frag界面初始化填充
     */
    fun initNavigationBody(context: FragmentActivity) {
        mPagerAdapter = FragmentPagerAdapter<BaseFragment<*, *>>(context).apply {
            for (index in 0 until fragList.value!!.size) {
                addFragment(fragList.value!![index])
            }
        }
        getNavigationBody().adapter = mPagerAdapter
    }
    /**
     * 数据规模，取最小值
     */
    fun initSize() {
        navigationSize = Math.min(itemList.value!!.size, fragList.value!!.size)
    }

    /**
     * 导航item点击事件
     */
    fun navigationItemClick(pos: Int) {
        mNavigationAdapter?.let {
            if (it.selectedPosition == pos) {
                return@let
            }
            if (it.listener == null || it.listener!!.onNavigationItemSelected(pos)) {
                it.selectedPosition = pos
                currTitle.value = titleList?.get(pos)
            }
        }
    }

    /**
     * 导航item选择事件,
     * 联动frag
     */
    override fun onNavigationItemSelected(position: Int): Boolean {
        return when (position) {
            in 0 until navigationSize -> {
                getNavigationBody().currentItem = position
                true
            }
            else -> false
        }
    }

    /**
     * 切换frag
     */
    fun switchFragment(fragmentIndex: Int) {
        if (fragmentIndex == -1) {
            return
        }
        when (fragmentIndex) {
            in 0 until navigationSize -> {
                getNavigationBody().currentItem = fragmentIndex
                mNavigationAdapter?.selectedPosition = fragmentIndex
                currTitle.value = titleList?.get(fragmentIndex)
            }
        }
    }

    /**
     * act销毁时释放
     */
    fun destroyAct() {
        getNavigationBody().adapter = null
        getNavigationBottom().adapter = null
        mNavigationAdapter?.listener = null
    }

    abstract fun getNavigationBottom(): RecyclerView
    abstract fun getNavigationBody(): NoScrollViewPager
}